#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	// Create the main window
	sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
	// Load the fonts
	sf::Font winter;
	if (!winter.LoadFromFile("fonts/winter.ttf")) return EXIT_FAILURE;

	sf::Font arialn;
	if (!arialn.LoadFromFile("fonts/arialn.ttf")) return EXIT_FAILURE;

	// Create clock
	sf::Clock clock;

	// Create a buffer to convert a to String
	ostringstream ss;
 
	// Create static texts and move them
	sf::Text title("Ice Hack", winter, 195); title.Move(50,50);
	sf::Text info("Being developed 2011-2016", arialn, 20); info.Move(50,500);

	//Create changing texts and position them
	sf::Text timeStr("", arialn, 20);  timeStr.Move(600,440);
	sf::Text frameStr("", arialn, 20); frameStr.Move(600,470);
	sf::Text fpsStr("", arialn, 20);   fpsStr.Move(600,500);

	// Create needed varibles and give them a standard value
	int time = 0;
	int frames = 0;
	int fps = 0;

	// Start the game loop
	while (window.IsOpened())
	{
		// Process events
		sf::Event event;
		while (window.PollEvent(event))
		{
			// Close window : exit
			if (event.Type == sf::Event::Closed)
				window.Close();
		}

		// Set time from clock
		time = clock.GetElapsedTime();
		// Stream time to string
		ss << "time: " << time;
		// Set the visible time
		timeStr.SetString(ss.str());
		// Clear the stream
		ss.str("");

		// Stream frames to string
		ss << "frames: " << frames;
		// Set the visible frames
		frameStr.SetString(ss.str());
		// Clear the stream
		ss.str("");
		// Increase the frames with 1
		frames++;

		if(time >= 200)
		{
			fps =(int)((float)frames*1000/time);
			clock.Reset();
			frames = 0;
		}
		// Stream fps to string
		ss << "fps: " << fps;
		// Set the visible fps
		fpsStr.SetString(ss.str());
		// Clear the stream
		ss.str("");
 
		// Clear screen
		window.Clear();
 
		// Draw the strings to the screen buffer
		window.Draw(title);
		window.Draw(info);
		window.Draw(timeStr);
		window.Draw(frameStr);
		window.Draw(fpsStr);
 
		// Update the window
		window.Display();
	}
 
	return EXIT_SUCCESS;
}